class Sqlc < Formula
  desc "Generate type safe Go from SQL"
  homepage "https://sqlc.dev/"
  url "https://github.com/sqlc-dev/sqlc/archive/refs/tags/v1.29.0.tar.gz"
  sha256 "7f26a9539d25df18d3cd2f02785b5a08e30adf568a08b50c5504556f16c1fb5d"
  license "MIT"
  head "https://github.com/sqlc-dev/sqlc.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "5b2cb8abc13302602bacb210046b3363ea5ffa62cec7f6450eb36db5d30329e1"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "5e5bbd8ba0116438e96980d549854ba347dc8e2964ec993dfa071ba0f0c10867"
    sha256 cellar: :any_skip_relocation, arm64_ventura: "1da3bcf4cc4bea4e1161f379d638b8e39d5daabe1d39bf006627286731aceeb6"
    sha256 cellar: :any_skip_relocation, sonoma:        "20699ef3244bbeb467fb5f04aefd76d5da9962d2f60d8ef949d68591d0e34698"
    sha256 cellar: :any_skip_relocation, ventura:       "9dd512414f694c9c0ce86230c24877d6d9a0d80f3967a8241e5cfdcec4556359"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "1673b21b686ec9fba8afe6c78343255389b33ddaa218f3e93f16c9422bf8174f"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "3f7b484597cfb48ac5eafbb2b5e04b8c02027719162aeb0b15eb6833c8781da4"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w"), "./cmd/sqlc"

    generate_completions_from_executable(bin/"sqlc", "completion")
  end

  test do
    (testpath/"sqlc.json").write <<~JSON
      {
        "version": "1",
        "packages": [
          {
            "name": "db",
            "path": ".",
            "queries": "query.sql",
            "schema": "query.sql",
            "engine": "postgresql"
          }
        ]
      }
    JSON

    (testpath/"query.sql").write <<~SQL
      CREATE TABLE foo (bar text);

      -- name: SelectFoo :many
      SELECT * FROM foo;
    SQL

    system bin/"sqlc", "generate"
    assert_path_exists testpath/"db.go"
    assert_path_exists testpath/"models.go"
    assert_match "// Code generated by sqlc. DO NOT EDIT.", File.read(testpath/"query.sql.go")
  end
end
